﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.DirectoryServices;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks; 
    #endregion

    /// <summary>
    /// A DNS A record, used to map a host name to an IPV4 address
    /// </summary>
    [Serializable]
    public class DNSARecord : DNSResourceRecord {

        #region Members
        /// <summary>
        /// The IPV4 Address
        /// </summary>
        public string IPAddress { get; private set; } 
        #endregion

        #region Constructor
        public DNSARecord() {
            this.Initialize();
            this.ProcessResults();
        }

        public DNSARecord(DNSResourceRecord parentRecord) {
            parentRecord.ShallowCopy<DNSResourceRecord, DNSARecord>(this);
            this.ProcessResults();
        }

        public DNSARecord(DirectoryEntry directoryEntry, bool destroyInputObject = true)
            : base(directoryEntry, destroyInputObject) {
            if (this.GetType().Name == typeof(DNSARecord).Name) {
                this.Attributes = null;
            }
        }

        public DNSARecord(SearchResult result, bool destroyInputObject = true)
            : base(result, destroyInputObject) {
                if (this.GetType().Name == typeof(DNSARecord).Name) {
                this.Attributes = null;
            }
        }
        #endregion

        #region Methods
        protected override void Initialize() {
            this.IPAddress = string.Empty;
        }

        internal override void ProcessDNSRecordStream(EndianBinaryReader propertyReader) {
            base.ProcessDNSRecordStream(propertyReader);

            this.IPAddress = propertyReader.ReadIPAddress().ToString();

            base.RecordData = this.IPAddress;
        }

        protected override void ProcessResults() {

            try {
                if (string.IsNullOrWhiteSpace(this.DN)) {
                    base.ProcessResults();
                }

                var memoryStream = new MemoryStream(this.DNSRecord);
                using (var propertyReader = new EndianBinaryReader(memoryStream)) {
                    this.ProcessDNSRecordStream(propertyReader);
                }

                this.IsValid = !string.IsNullOrWhiteSpace(this.DN)
                    && this.ObjectClass.Any(x => x.Equals(ObjectCategoryShortName, StringComparison.OrdinalIgnoreCase))
                    && (this.RecordType == DNSRecordType.A);
            }
            catch (Exception e) {
                Debug.WriteLine(string.Format("{0}  [ThreadID: {1}]  {2} Error: {3}", DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId, CustomExtensions.CurrentMethodName(), e.VerboseExceptionString()));
                throw;
            }

        }
        #endregion
    }
}
